package Base;

import java.util.ServiceConfigurationError;

/**
 * <p>Title BinarySearch</p>
 *
 * @author YiMing Liu
 * @date 2018/7/10 20:26
 * <p>Description:</p>
 */
public class BinarySearch {

    public static void main(String[] args) {
        int[] a = {1, 4, 6, 8, 9, 11, 45, 89, 99};
        // begin:0    last:9
        //第一趟： 89跟a[4]：9 比较
        // 第二趟： 89 跟a[7] 45比较
        System.out.println(search(a, 0, a.length, 45));
    }

    public static int search(int[] a, int begin, int end, int flag) {
        int middle = (begin + end) / 2;
        if (flag < a[middle]) {
            return search(a, begin, end - 1, flag);
        } else if (flag > a[middle]) {
            return search(a, begin + 1, end, flag);
        } else {
            return flag;
        }
    }

   /* public static int search(int [] a,int begin,int end,int flag){
        int middle = (begin + end) /2;
        if(flag < a[middle]){
            return search(a,begin,end-1,flag);
        }else if(flag > a[middle]){
            return search(a,begin+1,end,flag);
        }else   {
            return flag;
        }
    }*/

    /*public static int search(int [] a , int flag){
        int begain = 0;
        int last = a.length;
        for(int i = 0 ; i < a.length ; i++){

            int middle = (last + begain) / 2;
            if (a[middle] == flag){
                return flag;
            }
            if (flag > a[middle]){
                begain  = middle +1;
            }
            if (flag < a[middle]){
                last = middle -1;
            }
        }
        return  0;
    }*/
}
